﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using ZhongKeSite.Entity;

namespace ZhongKeSite.Common.Helper
{
    public class CommonHelper
    {
        /// <summary>
        /// 拼接得到文件预览地址
        /// </summary>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        public static string GetJoinUrl(string fileUrl)
        {
            if (!string.IsNullOrWhiteSpace(fileUrl))
            {
                string Result = string.Format("{0}/?id={1}", AppSettings.FileDomain, DESEncrypt.Encrypt(string.Format("{0}&{1}", fileUrl, DateTime.Now.ToString(CommonConst.LongDateFormat)), DESEncrypt.Key));
                return Result;
            }
            return string.Empty;
        }
        /// <summary>
        /// 拼接得到文件预览地址
        /// </summary>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        public static string GetJoinUrl(string fileUrl, string fileDownloadName)
        {
            if (!string.IsNullOrWhiteSpace(fileUrl))
            {
                string Result = string.Format("{0}/?id={1}&name={2}", AppSettings.FileDomain, DESEncrypt.Encrypt(string.Format("{0}&{1}", fileUrl, DateTime.Now.ToString(CommonConst.LongDateFormat)), DESEncrypt.Key), fileDownloadName);
                return Result;
            }
            return string.Empty;
        }
        /// <summary>
        /// 初始化返回值
        /// </summary>
        /// <returns></returns>
        public static ResultResponse Init()
        {
            return new ResultResponse() { success = false, msg = CommonConst.Fail };
        }
        /// <summary>
        /// 初始化返回值
        /// </summary>
        /// <returns></returns>
        public static ResultResponse<T> Init<T>()
        {
            return new ResultResponse<T>() { success = false, msg = CommonConst.Fail };
        }

        public static string GetRandomString(string key, int stringLength)
        {
            if (key == null || key.Length < 8)
            {
                key = "abcdefghijklmnopqrstuvwxyz1234567890";
            }

            int length = key.Length;
            StringBuilder randomString = new StringBuilder(length);
            for (int i = 0; i < stringLength; ++i)
            {
                randomString.Append(key[SetRandomSeeds(length)]);
            }

            return randomString.ToString();
        }

        private static int SetRandomSeeds(int length)
        {
            decimal maxValue = (decimal)long.MaxValue;
            byte[] array = new byte[8];
            new RNGCryptoServiceProvider().GetBytes(array);
            return (int)(Math.Abs(BitConverter.ToInt64(array, 0)) / maxValue * length);
        }
        /// <summary>
        /// 发放状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetOrderStatus(int status)
        {
            //状态 0：未提交  1：待一审  2：待二审  3：待放款，4：部分成功，5：已完成 ，8：驳回，9：关闭
            string statusString = "未提交";
            switch (status)
            {
                case 0:
                    statusString = "未提交";
                    break;
                case 1:
                case 2:
                    statusString = "待审核";
                    break;
                case 3:
                    statusString = "待放款";
                    break;
                case 4:
                    statusString = "部分成功";
                    break;
                case 5:
                    statusString = "已完成";
                    break;
                case 8:
                    statusString = "驳回";
                    break;
                case 9:
                    statusString = "关闭";
                    break;
            }
            return statusString;
        }
        /// <summary>
        /// 充值状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetTransferStatus(int status)
        {
            //状态 0：待审核  1：审核通过  2：审核不通过
            string statusString = "未提交";
            switch (status)
            {
                case 0:
                    statusString = "待审核";
                    break;
                case 1:
                    statusString = "审核通过";
                    break;
                case 2:
                    statusString = "审核不通过";
                    break;
            }
            return statusString;
        }
        /// <summary>
        /// 发放状态-后台使用
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetOrderStatusBack(int status)
        {
            //状态 0：未提交  1：待一审  2：待二审  3：待放款，4：部分成功，5：已完成 ，8：驳回，9：关闭
            string statusString = "未提交";
            switch (status)
            {
                case 0:
                    statusString = "未提交";
                    break;
                case 1:
                    statusString = "待一审";
                    break;
                case 2:
                    statusString = "待二审";
                    break;
                case 3:
                    statusString = "待放款";
                    break;
                case 4:
                    statusString = "部分成功";
                    break;
                case 5:
                    statusString = "已完成";
                    break;
                case 8:
                    statusString = "驳回";
                    break;
                case 9:
                    statusString = "关闭";
                    break;
            }
            return statusString;
        }

        /// <summary>
        /// 代理提现状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetAgentWithdrawStatus(int status)
        {
            //状态 0：待处理  1：转账成功  2：转账失败  3：审核不通过  99：申请代付
            string statusString = "待处理";
            switch (status)
            {
                case 0:
                    statusString = "待处理";
                    break;
                case 1:
                    statusString = "转账成功";
                    break;
                case 2:
                    statusString = "转账失败";
                    break;
                case 99:
                    statusString = "已提交代付申请";
                    break;
                case 3:
                    statusString = "审核不通过";
                    break;
            }
            return statusString;
        }

        /// <summary>
        /// 发票状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetInvoiceStatus(int status)
        {
            //状态 0：开票中  1：已开票  2：已拒绝  3：已废弃
            string statusString = "开票中";
            switch (status)
            {
                case 0:
                    statusString = "开票中";
                    break;
                case 1:
                    statusString = "已开票";
                    break;
                case 2:
                    statusString = "已拒绝";
                    break;
                case 3:
                    statusString = "已废弃";
                    break;
            }
            return statusString;
        }

        /// <summary>
        /// 签约状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetSignRecordStatus(int status)
        {
            //状态 1：签约成功    2：签约过期
            string statusString = "签约成功";
            switch (status)
            {
                case 1:
                    statusString = "签约成功";
                    break;
                case 2:
                    statusString = "签约过期";
                    break;
            }
            return statusString;
        }

        public static string PhoneNumberFormat(string phoneNumber)
        {
            if (string.IsNullOrWhiteSpace(phoneNumber))
            {
                return string.Empty;
            }
            if (phoneNumber.Trim().Length > 4)
            {
                phoneNumber = phoneNumber.Trim();
                return $"{phoneNumber.Substring(0, 3)}****{phoneNumber.Substring(phoneNumber.Length - 4, 4)}";
            }
            else
            {
                return phoneNumber;
            }
        }

        public static List<string> GetCurrentParentCodeList(IList<SysRight> list, string parentCode)
        {
            List<string> currentlist = new List<string>();
            var s1 = list.FirstOrDefault(a => a.ID == parentCode);
            if (s1 != null)
            {
                currentlist.Add(s1.ID);

                var s2 = list.FirstOrDefault(b => b.ID == s1.ParentCode);
                if (s2 != null)
                {
                    currentlist.Add(s2.ID);

                    var s3 = list.FirstOrDefault(c => c.ID == s2.ParentCode);
                    if (s3 != null)
                    {
                        currentlist.Add(s3.ID);

                        var s4 = list.FirstOrDefault(d => d.ID == s3.ParentCode);
                        if (s4 != null)
                        {
                            currentlist.Add(s4.ID);

                            var s5 = list.FirstOrDefault(e => e.ID == s4.ParentCode);
                            if (s5 != null)
                            {
                                currentlist.Add(s5.ID);
                            }
                        }
                    }
                }
            }
            return currentlist;
        }
    }
}
